package com.xncoding.design.observation.demo1;
/**
 * 消费者
 * @author Administrator
 */
public class Consumer2  implements Runnable {

	private String name;
	private final Storage s;
	
	public Consumer2(Storage s){
		this.s=s;
	}
	@Override
	public void run() {
		try{
			while(true){
				//s.getWorkQueue().size()巨消耗资源，不建议使用
				/*if(s.getWorkQueue().isEmpty()&&s.isEmpty()){//工作完成
					System.out.println("仓储空间为空");
	    			Thread.sleep(5000);;
	    		}*/
				if(s.isEmpty()){//工作完成
					System.out.println("仓储空间为空");
	    			Thread.sleep(2000);;
	    		}
				
				Product product=s.pop();
				if(product==null){
					continue;
				}
				System.out.println("消费者2已消费（"+product.toString()+").");
			}
		}catch(InterruptedException e){
			System.out.println(name+":异常");
			
		}finally{
			System.out.println(name+":完成工作");
		}
		
	}
	


}
